<?php
namespace Utuo;

class DistributionShopDonate
{

    const DONATE_TABLE = 'DistributionShopDonate';

    const DONATE_SUM_TABLE = 'DistributionShopDonateSum';

    const DONATE_PROJECT_TABLE = 'DistributionShopDonateProject';

    /**
     * 获取商品捐赠金额
     * 根据捐赠开关和商品捐赠金额获取该商品需要捐赠的金额，如果为0，则不用处理
     *
     * @param boolean $donateOn            
     * @param float $goodsDonateMoney            
     * @return float
     */
    public static function getDonateMoney($donateOn, $goodsDonateMoney)
    {
        return $donateOn ? max(floatval($goodsDonateMoney), 0) : 0;
    }

    /**
     * 获取捐赠公益项目列表
     * 
     * @param unknown $cid            
     */
    public static function getDonateProjectLists($cid, $getDonateMoney = false)
    {
        $lists = M(self::DONATE_PROJECT_TABLE)->where(array(
            'cid' => $cid,
            'is_deleted' => 0
        ))->select();
        if ($getDonateMoney) {
            foreach ($lists as &$v) {
                $v['donateSum'] = self::getSum(array(
                    'cid' => $cid,
                    'projectId' => $v['id']
                ));
            }
        }
        return $lists;
    }

    /**
     * 获取捐赠列表
     *
     * @param array $condition
     *            三种用法：
     *            1. 获取某一客户的捐赠列表 $condition = array('cid'=>$cid)
     *            2. 获取某一用户的捐赠列表 $condition = array('cid'=>$cid,'uid'=>$uid)
     *            3. 获取某一产品的捐赠列表 $condition = array('cid'=>$cid,'goodsId'=>$goodsId)
     *            4. 获取某一项目的捐赠列表 $condition = array('cid'=>$cid,'projectId'=>$projectId)
     *            
     * @return array('count'=>$count,'lists'=>$lists)
     */
    public static function getLists($condition, $page, $pagesize)
    {
        $alias = "d";
        $where = array();
        foreach ($condition as $k => $v) {
            $where["$alias.$k"] = $v;
        }
        $m = M(self::DONATE_TABLE . " $alias");
        $m->where($where);
        
        // 总量
        $clone = clone $m;
        $count = $clone->count();
        
        $m->join("LEFT JOIN __WECHAT_USER__ u ON d.uid=u.id");
        $m->join("LEFT JOIN __DISTRIBUTION_SHOP_GOODS__ g ON d.goodsId=g.id");
        $m->join('LEFT JOIN __DISTRIBUTION_SHOP_ORDER__ o ON d.orderId=o.id');
        $m->join('LEFT JOIN __DISTRIBUTION_SHOP_DONATE_PROJECT__ p ON d.projectId=p.id');
        $m->field('d.*, u.nickname AS userName,u.face AS userFace,g.name AS goodsName,o.order_id AS orderId,p.name AS projectName');
        $lists = $m->select();
        return array(
            'count' => $count,
            'lists' => $lists
        );
    }

    /**
     * 添加捐赠
     *
     * @param unknown $cid            
     * @param unknown $uid            
     * @param unknown $orderId            
     * @param unknown $goodsId            
     * @param unknown $money            
     * @return unknown|boolean
     */
    public static function add($cid, $uid, $orderId, $goodsId, $goodsNumber, $donateMoney, $projectId = 0)
    {
        $totalMoney = floatval($goodsNumber * $donateMoney);
        if ($totalMoney <= 0) {
            return false;
        }
        $m = M(self::DONATE_TABLE);
        $data = array(
            'cid' => $cid,
            'uid' => $uid,
            'orderId' => $orderId,
            'goodsId' => $goodsId,
            'projectId' => $projectId
        );
        if (0 == $m->where($data)->count()) {
            $data['goodsNumber'] = $goodsNumber;
            $data['donateMoney'] = $donateMoney;
            $data['totalMoney'] = $totalMoney;
            $result = $m->add($data);
            if ($result) {
                self::incSum($cid, $uid, $goodsId, $totalMoney, $projectId);
            }
            return $result;
        }
        return false;
    }

    /**
     * 移除捐赠
     *
     * @param unknown $cid            
     * @param unknown $uid            
     * @param unknown $orderId            
     * @param unknown $goodsId            
     * @param unknown $money            
     */
    public static function remove($cid, $uid, $orderId, $goodsId, $goodsNumber, $donateMoney, $projectId = 0)
    {
        $m = M(self::DONATE_TABLE);
        $data = array(
            'cid' => $cid,
            'uid' => $uid,
            'orderId' => $orderId,
            'goodsId' => $goodsId
        );
        if ($m->where($data)->delete()) {
            self::decSum($cid, $uid, $goodsId, floatval($goodsNumber * $donateMoney), $projectId);
        }
    }

    /**
     * 获取捐赠总量
     *
     * @param array $condition
     *            三种用法：
     *            1. 获取某一客户的捐赠总量 $condition = array('cid'=>$cid)
     *            2. 获取某一用户的捐赠总量 $condition = array('cid'=>$cid,'uid'=>$uid)
     *            3. 获取某一产品的捐赠总量 $condition = array('cid'=>$cid,'goodsId'=>$goodsId)
     *            4. 获取某一项目的捐赠总量 $condition = array('cid'=>$cid,'projectId'=>$projectId)
     */
    public static function getSum($condition)
    {
        $sum = M(self::DONATE_SUM_TABLE)->where($condition)->sum('money');
        return floatval($sum);
    }

    /**
     * 增加总量
     *
     * @param unknown $cid            
     * @param unknown $uid            
     * @param unknown $goodsId            
     * @param unknown $money            
     */
    public static function incSum($cid, $uid, $goodsId, $money, $projectId)
    {
        $m = M(self::DONATE_SUM_TABLE);
        $condition = array(
            'cid' => $cid,
            'uid' => $uid,
            'goodsId' => $goodsId,
            'projectId' => $projectId
        );
        $data = $m->where($condition)->find();
        if ($data) {
            $data['money'] += $money;
            $m->save($data);
        } elseif ($money > 0) {
            $condition['money'] = $money;
            $m->add($condition);
        }
    }

    /**
     * 减少总量
     *
     * @param unknown $cid            
     * @param unknown $uid            
     * @param unknown $goodsId            
     * @param unknown $money            
     */
    public static function decSum($cid, $uid, $goodsId, $money, $projectId)
    {
        M(self::DONATE_SUM_TABLE)->where(array(
            'cid' => $cid,
            'uid' => $uid,
            'goodsId' => $goodsId,
            'projectId' => $projectId
        ))->setDec('money', $money);
    }
}